/**
 * Created by admin on 2017/6/8.
 */
import {
  RegisterDetail,

} from '../mutation-types';
import { Cache, cache_keys } from '../../utils/util';


// 查询我的活动列表
export const queryMyActivity =function({commit,state},data){
    let params ={},
    index=data.index || 0,
    page=data.page || 1,
    canceled=data.canceled || 1;
    switch (true){
      case index==4:
         params.canceled =canceled;
         break;
      case index>0:
         params.status =index;
         break;
    }
    params.page =page;
    let oauth = Cache.get(cache_keys.token);
    EventBus.$http.get(EventBus.$Config.baseUrl+'api/activity/myList',{
      headers: {
        Authorization: oauth.access_token
      },
      params
    }).then(res =>{
        if(res.data.status){
            EventBus.$emit('my-activityList',res.data,data.index);
        }
        else{
          EventBus.$Modal.info({title:"消息提示",body:res.data.message || '加载错误'});
        }
    },()=>{
      EventBus.$Modal.info({title:"消息提示",body: '加载失败'});
    })
};

// 查询我的报名详情
export const queryRegisterDetail =function({commit,state},id){
   let oauth =Cache.get(cache_keys.token);
   EventBus.$http.get(EventBus.$Config.baseUrl+'api/activity/myActivity/' + id,{
      headers:{
        Authorization: oauth.access_token
      }
   }).then( res=>{
      if(res.data.status){
         commit(RegisterDetail,res.data);
      }
      else{
          EventBus.$Modal.info({title:"消息提示",body:"加载错误"});
      }
   },()=>{
      EventBus.$Modal.indo({title:"消息提示",body:"加载失败"});
   })
};


// 取消报名
export const cancelRegister =function({commit,state},id){
   let oauth =Cache.get(cache_keys.token);
  EventBus.$http.post(EventBus.$Config.baseUrl+'api/activity/cancel/'+id,{},{
    headers: {
      Authorization: oauth.access_token
    }
  }).then(res=>{
     if(res.data.status){
          EventBus.$emit("cancel-complete");
     }
     else{
       EventBus.$Modal.info({title:"消息提示",body:res.data.message || "取消失败"});
     }
  },()=>{
    EventBus.$Modal.info({title:"消息提示",body:"加载失败"});
  });
}

// 签到
export const signIn =function({commit,state},data){
    let oauth =Cache.get(cache_keys.token);
    EventBus.$http.post(EventBus.$Config.baseUrl+'api/activity/sign/'+data.id,{
         code:data.code
    },{
        headers: {
          Authorization: oauth.access_token
        }
    }).then(res=>{
       if(res.data.status){
         Cache.set(cache_keys.sign_gain_coupon, res.data.data);
         EventBus.$emit('sign-complete');
       }else{
         EventBus.$Modal.info({title:"消息提示",body:res.data.message || '签到失败，请重试'});
       }
    },()=>{
      EventBus.$Modal.info({title:"消息提示",body: '加载失败'});
    })
}
